﻿using System;
using System.Linq;
using LinqCustomCompiler.Model;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;

namespace LinqCustomCompiler
{
  public class Program
  {
    public static void Main(string[] args)
    {
      // Loading domain configuration section with name "Default".
      var domainConfiguration = DomainConfiguration.Load("Default");
      var domain = Domain.Build(domainConfiguration);

      using (var session = domain.OpenSession())
      using (var tx = session.OpenTransaction()) {
        // Create some orders
        new Order(session) {ItemPrice = 10m, ItemCount = 1};
        new Order(session) {ItemPrice = 3m, ItemCount = 10};
        new Order(session) {ItemPrice = 1m, ItemCount = 12};

        // Get all orders sorted by computed property
        Console.WriteLine("Orders sorted by total price (desc):");
        var query = session.Query.All<Order>().OrderByDescending(o => o.TotalPrice);
        foreach (var order in query)
          Console.WriteLine(order);

        // Filter orders using custom method
        Console.WriteLine("Orders with total price higher than 15:");
        var query2 = session.Query.All<Order>().Where(o => o.TotalPrice.GreaterThan(15));
        foreach (var order in query2)
          Console.WriteLine(order);
      }
    }
  }
}
